package com.ebs.md.fxmonitor.marketmonitor;

/**
 * Perforce Revision:
 *  $File: //depot/MarketData/fxmonitor/main/src/main/java/com/ebs/md/fxmonitor/marketmonitor/CommonPayloadParse.java $
 *  $Revision: #12 $  $Change: 116234 $  $DateTime: 2012/10/25 13:41:58 $  $Author: Arik.Sher $
 */

import java.util.Collections;
import java.util.List;

import com.ebs.commons.comp.IParsedPayload;
import com.ebs.marketdata.dictionary.MatchingType;
import com.ebs.md.api.IBookEntry;
import com.ebs.md.api.OrderSide;
import com.ebs.md.api.Rate;
import com.ebs.md.api.product.PairProduct;
import com.ebs.md.fxmonitor.orders.TopOfBooksChangeListener;

/**
 * 
 * helps parsing the Payload output, done by TopOfBooksChangeListener.
 * 
 * @see TopOfBooksChangeListener
 */
public final class CommonPayloadParse
{
	public static final String ACTION_ADD_STR    = "ADD";
	public static final String ACTION_REMOVE_STR = "REMOVE";
	
	/**
	 * 
	 * @see com.ebs.md.fxmonitor.orders.TopOfBooksChangeListener#createNotificationPayloadByOrder
	 */
	private CommonPayloadParse() {}

	
	public static String  getMarketIdAsStr(IParsedPayload payload) {
		String  baseCcyIso  = getBaseCurrencyIso (payload);
		String localCcyIso  = getLocalCurrencyIso(payload);
		int    mathcingType = getMatchingTypeId  (payload);
		
		return String.format("%s/%s_%d", baseCcyIso, localCcyIso, mathcingType);
	}
	
	public static PairProduct  getPair(IParsedPayload payload) {
		return (PairProduct)payload.getParsedField("pair_product");
	}
	
	public static String  getBaseCurrencyIso(IParsedPayload payload) {
		return payload.getParsedStringField( "base_ccy_iso");
	}
	
	public static  String getLocalCurrencyIso(IParsedPayload payload) {
		return payload.getParsedStringField("local_ccy_iso");
	}

	public static  int    getMatchingTypeId(IParsedPayload payload) {
		return payload.getParsedIntField   ("matching_type");
	}
	
	public static  MatchingType    getMatchingType(IParsedPayload payload) {
		int matchingTypeId = getMatchingTypeId(payload);
		return MatchingType.getById(matchingTypeId);
	}
	
	public static  String getOrderId(IParsedPayload payload) {
		return payload.getParsedStringField("order_id");
	}
	
	public static  long getOrderAmount(IParsedPayload payload) {
		return payload.getParsedLongField("order_amount");
	}
	
	public static  long getOrderTotalAmount(IParsedPayload payload) {
		return payload.getParsedLongField("order_total_amount");
	}
	
	public static  List<Rate> getTopBidRatesList(IParsedPayload payload) {
		return (List<Rate>)payload.getParsedField("top_bid_rates_list");
	}
	
	public static  List<Rate> getTopOfferRatesList(IParsedPayload payload) {
		return (List<Rate>)payload.getParsedField("top_offer_rates_list");
	}
	
	public static  Rate getBidTopRate(IParsedPayload payload) {
		return (Rate)payload.getParsedField("bid_top_rate");
	}
	
	public static  Rate getOfferTopRate(IParsedPayload payload) {
		return (Rate)payload.getParsedField("offer_top_rate");
	}

	public static  Rate getPreviousBidTopRate(IParsedPayload payload) {
		return (Rate)payload.getParsedField("prev_bid_top_rate");
	}

	public static  Rate getPreviousOfferTopRate(IParsedPayload payload) {
		return (Rate)payload.getParsedField("prev_offer_top_rate");
	}

	public static  Rate getOrderRate(IParsedPayload payload) {
		return (Rate)payload.getParsedField("order_rate");
	}

	public static  OrderSide getOrderSide(IParsedPayload payload) {
		return OrderSide.byLegcayCode(
				payload.getParsedIntField  ("qte_type"));
	}

	public static  String getActionName(IParsedPayload payload) {
		return payload.getParsedStringField("action_name");
	}

	public static boolean isActionAdd   (IParsedPayload payload) {
		return ACTION_ADD_STR    .equals( getActionName(payload));
	}

	public static boolean isActionRemove(IParsedPayload payload) {
		return ACTION_REMOVE_STR .equals( getActionName(payload));
	}
	
	public static  long   getActionTime(IParsedPayload payload) {
		return payload.getParsedLongField  ("action_time");
	}
	
	public static String getRegion(IParsedPayload payload) {
		return payload.getParsedStringField("trader_region");
	}

	public static Rate getTopRateBySide(IParsedPayload payload, OrderSide orderSide) {
		if ( OrderSide.BID.equals  (orderSide) ) {
			return  getBidTopRate  (payload);
		}
		if ( OrderSide.OFFER.equals(orderSide) ) {
			return  getOfferTopRate(payload);
		}
		return null;
	}

	public static Rate getPreviousTopRateBySide(IParsedPayload payload, OrderSide orderSide) {
		if ( OrderSide.BID.equals  (orderSide) ) {
			return  getPreviousBidTopRate  (payload);
		}
		if ( OrderSide.OFFER.equals(orderSide) ) {
			return  getPreviousOfferTopRate(payload);
		}
		return null;
	}
	
	public static  List<Rate> getTopRatesListBySide(IParsedPayload payload, OrderSide orderSide) {
		if (   OrderSide.BID.equals  (orderSide) ) {
			return getTopBidRatesList(payload);
		}
		if (   OrderSide.OFFER.equals(orderSide) ) {
			return getTopOfferRatesList(payload);
		}
		return Collections.emptyList();
	}

	public static <BE extends IBookEntry> List<BE> getBestEntriesListBySide(IParsedPayload payload, OrderSide orderSide) {
		String fieldNameOfBookEntries = "book_entries_of_top_offer";
		if ( OrderSide.BID.equals(orderSide) ) {
			fieldNameOfBookEntries    = "book_entries_of_top_bid";
		}
		return (List<BE>)payload.getParsedField(fieldNameOfBookEntries) ;
	}
	
}

